package com.zc.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zc.servicebase.exception.CongException;
import com.zc.ucenter.entity.vo.RegisterVo;
import com.zc.ucenter.mapper.MemberMapper;
import com.zc.ucenter.service.MemberService;
import com.zc.utils.JwtUtils;
import com.zc.utils.MD5;
import com.zc.utils.ResultStatus;
import com.zc.utils.vo.Member;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author cong
 * @since 2022-12-01
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

	@Autowired
	private RedisTemplate<String, String> redisTemplate;

	@Override
	public String login(Member member) {
		String mobile = member.getMobile();
		String password = member.getPassword();
		if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
			throw new CongException(ResultStatus.ERROR, "手机号或密码为空");
		}

		QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("mobile", mobile);
		member = this.getOne(queryWrapper);
		if (member == null) {
			throw new CongException(ResultStatus.ERROR, "用户不存在");
		}

		if (!MD5.encrypt(password).equals(member.getPassword())) {
			throw new CongException(ResultStatus.ERROR, "密码不正确");
		}

		if (member.getIsDisabled()) {
			throw new CongException(ResultStatus.ERROR, "用户被禁用");
		}

		String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());
		return token;
	}

	@Override
	public boolean registerUser(RegisterVo registerVo) {
		String code = registerVo.getCode();
		String mobile = registerVo.getMobile();
		String nickname = registerVo.getNickname();
		String password = registerVo.getPassword();

		if (StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile)
				|| StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)) {
			throw new CongException(ResultStatus.ERROR, "请将信息填写完整");
		}

		// 验证码是否正确
		String msgCode = redisTemplate.opsForValue().get(mobile);
		if (!msgCode.equals(code)) {
			throw new CongException(ResultStatus.ERROR, "验证码错误");
		}

		// 查询库中是否存在该用户
		QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("mobile", mobile);
		int count = this.count(queryWrapper);
		if (count > 0) {
			throw new CongException(ResultStatus.ERROR, "用户已注册");
		}

		Member member = new Member();
		member.setNickname(nickname);
		member.setPassword(MD5.encrypt(password));
		member.setMobile(mobile);
		member.setAvatar("");
		member.setIsDisabled(false);
		boolean save = this.save(member);
		return save;
	}

	@Override
	public Member getByOpenid(String openid) {
		QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("openid", openid);
		Member member = this.getOne(queryWrapper);
		return member;
	}
}
